package com.example.superoa.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.pojos.SalaryAccout;
import com.example.superoa.model.pojos.SalaryPay;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.MenuService;
import com.example.superoa.model.service.PayService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class PayController {
	@Autowired
	private PayService ps;	

	@Autowired
	private MenuService m_ser;
	
	@RequestMapping("salary-pay")
	public void test1() {
		
	}
	@RequestMapping("show-pay")
	@ResponseBody
	public Map<String,Object> test2(int page,int rows) {
		Map<String,Object> map = new HashMap<>();
			//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray i=ps.findpay();
		for (Object object : i) {
			JSONObject obj = (JSONObject) object;
			Date sp_month = (Date) obj.get("sp_month");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sp_month);
			int month=cal.get(Calendar.MONTH);
			int year=cal.get(Calendar.YEAR);
			int months=month+1;
			String my=year+"-"+months;			
			obj.put("sp_xzmonth", my);
			if(obj.get("sub_static").equals(1)) {
				obj.put("sub_static", "审核通过");				
			}else if(obj.get("sub_static").equals(0)){
				obj.put("sub_static", "待审核");
			}else if(obj.get("sub_static").equals(2)){
				obj.put("sub_static", "驳回");
			}else if(obj.get("sub_static").equals(3)){				
				obj.put("sub_static", "已发放");
			}else {
				obj.put("sub_static", "状态错误");
			}
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	@RequestMapping("selectthems")
	@ResponseBody
	public JSONArray selectthem() {
		JSONArray i = ps.findpay();
		for (Object object : i) {
			JSONObject obj = (JSONObject) object;
			if(obj.get("sub_static").equals(1)) {
				obj.put("sub_static", "审核通过");				
			}else if(obj.get("sub_static").equals(0)){
				obj.put("sub_static", "待审核");
			}else if(obj.get("sub_static").equals(2)){
				obj.put("sub_static", "驳回");
			}else if(obj.get("sub_static").equals(3)){				
				obj.put("sub_static", "已发放");
			}else {
				obj.put("sub_static", "状态错误");
			}
		}
		return i;
	}
	@RequestMapping("find-thems")
	@ResponseBody
	public Map<String,Object> test4(int page,int rows,int account_id) {
		Map<String,Object> map = new HashMap<>();
			//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		System.err.println(account_id);
		JSONArray i=ps.findthems(account_id);
		for (Object object : i) {
			JSONObject obj = (JSONObject) object;
			Date sp_month = (Date) obj.get("sp_month");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sp_month);
			int month=cal.get(Calendar.MONTH);
			int year=cal.get(Calendar.YEAR);
			String my=year+"-"+month+1;			
			obj.put("sp_xzmonth", my);
			if(obj.get("sub_static").equals(1)) {
				obj.put("sub_static", "审核通过");				
			}else if(obj.get("sub_static").equals(0)){
				obj.put("sub_static", "待审核");
			}else if(obj.get("sub_static").equals(2)){
				obj.put("sub_static", "驳回");
			}else if(obj.get("sub_static").equals(3)){				
				obj.put("sub_static", "已发放");
			}else {
				obj.put("sub_static", "状态错误");
			}
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	//添加
	@PostMapping("tianjia-pay")
	@ResponseBody
	public void tianjia(SalaryPay sp) {
		 ps.tianjia(sp);
	}
	//发放查看
	@RequestMapping("pay-ck")
	@ResponseBody	
	public JSONArray payCk(int account_id) {
		return ps.pay_chakan(account_id);
	}	
	//套账所属员工
	@RequestMapping("pay-cks")
	@ResponseBody	
	public JSONArray payCks(int account_id) {
		return ps.pay_accuntname(account_id);
	}
	@RequestMapping("delect-pay")
	@ResponseBody
	public void delect(int pay_id) {
		ps.shanchu(pay_id);
	}
	@RequestMapping("salary-paymingxi")
	public void mx(int pay_id,Model model) {		
		model.addAttribute("pay",pay_id);	
	}
	@RequestMapping("select-accounts")
	@ResponseBody
	public JSONArray selectcounts() {
		return ps.selectcounts();
	}
	//新增
	@RequestMapping("xz-pay")
	@ResponseBody
	public void xinzengpay(SalaryPay spay,int sa_id,int staff_id) {
		ps.xinzengpay(spay, sa_id, staff_id);
	}
	@PostMapping("mingxidpays")
	@ResponseBody
	public void mingxiss(int account_id,int sub_id,int pay_id) {
		ps.kq_month(account_id,sub_id,pay_id);
	}
	@RequestMapping("mingxid")
	@ResponseBody
	public Map<String,Object> test5(int page,int rows,int pay_id) {
		Map<String,Object> map = new HashMap<>();
			//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray i=ps.payde(pay_id);		
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	@RequestMapping("xzpay-add")
	@ResponseBody
	public void xzpay(String sp_thems,int account_id,HttpSession session) {
		Integer id=Integer.parseInt(session.getAttribute("sid").toString());
		ps.xzpay(sp_thems, account_id,id);
	}
	@RequestMapping("xzsubs")
	@ResponseBody
	public void xzsub(int sub_id,String type,String reason,HttpSession session) {
		Integer id=Integer.parseInt(session.getAttribute("sid").toString());
		ps.xinzengss(sub_id,1,id,type,reason);
	}
	@RequestMapping("payaccounts")
	@ResponseBody
	public JSONArray findaccounts() {
		JSONArray payaccout = ps.payaccount();
		int j=1;
		for (Object object : payaccout) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhao", j);
			j++;
		}
		return payaccout;
	}
	@RequestMapping("findaccountselect")
	@ResponseBody
	public JSONArray findaccountselect() {
		return ps.findaccountselect();
	}
}

